package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.ItemTransferAction;
import gatperdut.chato.core.interpreter.grammar.comminfo.ItemTransferInfo;
import gatperdut.chato.core.item.ItemController;

public class GiveAction extends ItemTransferAction {

    public GiveAction( ItemTransferInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        if ( emptyEmote() ) {
            getEntityController().tx( "Empty emotes are not allowed." );
            return CommandExecutionResult.OK;
        }

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        if ( !checkUnindexedWord( getInfo().getWhat() ) ) {
            return CommandExecutionResult.OK;
        }

        synchronized ( getEntityController().getInventoryController() ) {
            ItemController ic = getEntityController().getInventoryController().getHeldItemController( getInfo().getWhat() );
            if ( ic == null ) {
                getEntityController().tx( "You don't seem to be holding that." );
                return CommandExecutionResult.OK;
            }

            synchronized ( getEntityController().getRoomController().getCharactersController() ) {
                BaseController whomc = getEntityController().getRoomController().getCharactersController().getEntityController( getInfo().getWhom() );
                if ( whomc == null ) {
                    getEntityController().tx( "You don't see that character here." );
                    return CommandExecutionResult.OK;
                }

                if ( whomc == getEntityController() ) {
                    getEntityController().tx( "You can't give objects to yourself." );
                    return CommandExecutionResult.OK;
                }

                synchronized ( whomc.getInventoryController() ) {
                    if ( !whomc.isAcceptGive() ) {
                        getEntityController().getEchoes().txGiveNotAccepted( whomc, ic );
                        return CommandExecutionResult.OK;
                    }

                    if ( !whomc.getInventoryController().hasOneFreeHand() ) {
                        getEntityController().getEchoes().txGiveHandsFull( whomc, ic );
                        return CommandExecutionResult.OK;
                    }

                    getEntityController().getInventoryController().takeFromInventory( ic );

                    whomc.getInventoryController().take( ic );

                    getEntityController().getEchoes().txGive( ic, whomc, emote );
                }
            }
        }
        return CommandExecutionResult.OK;
    }

}
